package com.cskaoyan.util;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.util.List;

public class ValidateUtils {
    /**
     * 返回参数校验失败的信息
     * @param bindingResult
     * @return
     */
    //TODO
    public static BaseRespVo valid(BindingResult bindingResult) {

        FieldError fieldError = bindingResult.getFieldError();
        String field = fieldError.getField();
        Object rejectedValue = fieldError.getRejectedValue();
        String defaultMessage = fieldError.getDefaultMessage();
        String messageFormat = "rejected : %s=%s => %s";
        return BaseRespVo.fail(String.format(messageFormat, field, rejectedValue, defaultMessage));
    }
}
